﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>FunctoinTree 权限分配树</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <link href="../demo.css" rel="stylesheet" type="text/css" />

    <script src="../../scripts/boot.js" type="text/javascript"></script>

    <style>
    .function-item 
    {
        margin-left:5px;
        margin-right:5px;
    }
    .function-item input
    {
        vertical-align:bottom;
    }
    </style>
    
</head>
<body>
    <h1>FunctoinTree 权限分配树</h1>
    
<input type="button" value="获取权限" onclick="getFuns()" />
<div id="treegrid1" class="mini-treegrid" style="width:700px;height:auto;"     
    url="functiontree.txt"
    treeColumn="name" idField="id" parentField="pid" resultAsTree="false"  
    allowResize="true" expandOnLoad="true" showTreeIcon="true"
    allowSelect="false" allowCellSelect="false" enableHotTrack="false"
    ondrawcell="ondrawcell" 
>
    <div property="columns">
        <div type="indexcolumn"></div>
        <div name="name" field="name" width="120" >模块名称</div>
        <div field="functions" width="100%">权限</div>
    </div>
</div>


</body>
</html>
<script>
    mini.parse();
    var tree = mini.get("treegrid1");


    function ondrawcell(e) {
        var tree = e.sender,
            record = e.record,            
            column = e.column,
            field = e.field,
            id = record[tree.getIdField()],
            funs = record.functions;

        function createCheckboxs(funs) {
            if (!funs) return "";
            var html = "";
            for (var i = 0, l = funs.length; i < l; i++) {
                var fn = funs[i];
                var clickFn = 'checkFunc(\'' + id + '\',\'' + fn.action + '\', this.checked)';
                var checked = fn.checked ? 'checked' : '';
                html += '<label class="function-item"><input onclick="' + clickFn + '" ' + checked + ' type="checkbox" name="'
                        + fn.action + '" hideFocus/>' + fn.name + '</label>';
            }
            return html;
        }

        if (field == 'functions') {
            e.cellHtml = createCheckboxs(funs);
        }
    }

    function getFuns() {
        var data = tree.getData();
        var json = mini.encode(data);
        alert(json);
    }
    function checkFunc(id, action, checked) {
        var record = tree.getRecord(id);
        if(!record) return;
        var funs = record.functions;
        if (!funs) return;
        function getAction(action) {
            for (var i = 0, l = funs.length; i < l; i++) {
                var o = funs[i];
                if (o.action == action) return o;
            }
        }
        var obj = getAction(action);
        if (!obj) return;
        obj.checked = checked;
    }

</script>